package com.wang.springbootinit.controller.admin;

import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.model.entity.Activity;
import com.wang.springbootinit.service.ActivityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 活动管理
 */
@RestController
@RequestMapping("/admin/activity")
public class AdminActivityController {
    @Resource
    private ActivityService activityService;

    /**
     * 创建活动
     *
     * @param activity 活动信息
     * @return 新创建的活动ID
     */
    @PostMapping("/add")
    public BaseResponse<Long> addActivity(@RequestBody Activity activity) {
        long activityId = activityService.addActivity(activity);
        return new BaseResponse<>(0, activityId, "添加成功");
    }

    /**
     * 根据ID获取活动详情
     *
     * @param id 活动ID
     * @return 活动信息
     */
    @GetMapping("/get")
    public BaseResponse<Activity> getActivity(@RequestParam Long id) {
        Activity activity = activityService.getActivity(id);
        return new BaseResponse<>(0, activity, "获取成功");
    }

    @GetMapping("/participants")
    public BaseResponse<Activity> getParticipants(@RequestParam Long activityId) {
        Activity activity = activityService.getActivity(activityId);
        return new BaseResponse<>(0, activity, "获取成功");
    }

    /**
     * 分页获取活动列表
     *
     * @param page 当前页码
     * @param pageSize 每页显示条数
     * @return 活动列表
     */
    @GetMapping("/list")
    public BaseResponse<List<Activity>> listActivities(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Integer status
            ) {
        List<Activity> activityList = activityService.listActivities(page, pageSize,name,type,status);
        return new BaseResponse<>(0, activityList, "获取成功");
    }

    /**
     * 更新活动信息
     *
     * @param activity 需要更新的活动信息
     * @return 是否更新成功
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateActivity(@RequestBody Activity activity) {
        boolean result = activityService.updateActivity(activity);
        return new BaseResponse<>(0, result, "更新成功");
    }

    /**
     * 删除活动
     *
     * @param id 要删除的活动ID
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteActivity(@RequestParam Long id) {
        boolean result = activityService.deleteActivity(id);
        return new BaseResponse<>(0, result, "删除成功");
    }
}
